package com.sun.xacml.attr;

import java.net.URI;
import java.util.List;

import org.jdom.Element;
import org.jdom.Namespace;
import org.jdom.input.DOMBuilder;
import org.w3c.dom.Node;

import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.Envelope;

public class BoxAttribute extends AttributeValue {
	private static URI identifierURI;
	private static String srsName;
	private static Envelope geometry;

	/**
	 * Constructor tao Box attribute tu jdomRoot
	 * <Box></Box>
	 */	
	public BoxAttribute(Element root) {
		super(identifierURI);

		Namespace gml = null;
		List additionalNamespaces = root.getAdditionalNamespaces();
		for (int ix = 0; ix < additionalNamespaces.size(); ix++)
			if ("gml".equals(((Namespace) additionalNamespaces.get(ix))
					.getPrefix()))
				gml = (Namespace) additionalNamespaces.get(ix);

		//lay srsName
		srsName = root.getAttributeValue("srsName");
		
		//qua trinh lay toa do cua Box
		List<Element> coordList = root.getChildren();
		List<Coordinate> point = null;
		Element coord = root.getChild("coord");
		for (int i = 0; i < coordList.size() - 1; i++) {
			if (coord != null) {
				double X = Double.parseDouble(coordList.get(i).getChild("X",gml).getValue());
				double Y = Double.parseDouble(coordList.get(i).getChild("Y",gml).getValue());
				point.set(i, new Coordinate(X, Y));
			} 
			else {
				String str = root.getChild("coordinate", gml).getValue();
				String[] coordinateList = str.split(",");
				String[] toado;
				for (int j = 0; j < coordinateList.length; j++) {
					toado = coordinateList[i].split(" ");
					point.set(j, new Coordinate(Integer.parseInt(toado[0]),	Integer.parseInt(toado[1])));
				}
			}
		}
		
		//chuyen sang jts
		geometry = new Envelope(point.get(0), point.get(1));
	}

	/**
	 * Constructor tao Box attribute tu DomRoot
	 * 
	 */
	public BoxAttribute(org.w3c.dom.Element domRoot) {
		super(identifierURI);

		//chuyen tu DOM sang jdom
		DOMBuilder domBuilder = new DOMBuilder();
		org.jdom.Element jdomRoot = domBuilder.build(domRoot);
		
		Namespace gml = null;
		List additionalNamespaces = jdomRoot.getAdditionalNamespaces();
		for (int ix = 0; ix < additionalNamespaces.size(); ix++)
			if ("gml".equals(((Namespace) additionalNamespaces.get(ix))
					.getPrefix()))
				gml = (Namespace) additionalNamespaces.get(ix);

		//lay srsName
		srsName = jdomRoot.getAttributeValue("srsName");
		
		//qua trinh lay toa do cua Box
		List<Element> coordList = jdomRoot.getChildren();
		List<Coordinate> point = null;
		Element coord = jdomRoot.getChild("coord");
		for (int i = 0; i < coordList.size() - 1; i++) {
			if (coord != null) {
				double X = Double.parseDouble(coordList.get(i).getChild("X",gml).getValue());
				double Y = Double.parseDouble(coordList.get(i).getChild("Y",gml).getValue());

				point.set(i, new Coordinate(X, Y));
			} else {
				String str = jdomRoot.getChild("coordinate", gml).getValue();
				String[] coordinateList = str.split(",");
				String[] toado;
				for (int j = 0; j < coordinateList.length; j++) {
					toado = coordinateList[i].split(" ");
					point.set(j, new Coordinate(Integer.parseInt(toado[0]),Integer.parseInt(toado[1])));
				}
			}
		}
		
		//tao gia tri ben jts
		geometry = new Envelope(point.get(0), point.get(1));
	}
	
	//get geometry
	public Envelope getGeometry() {
		return geometry;
	}

	//get SrsName
	public String getSrsName() {
		return srsName;
	}

	@Override
	public String encode() {
		// TODO Auto-generated method stub
		return ("Box(" + String.valueOf(geometry.getMaxX()) + " "
				+ String.valueOf(geometry.getMaxY()) + ","
				+ String.valueOf(geometry.getMinX()) + " "
				+ String.valueOf(geometry.getMinY()) + ")");
	}

	public static AttributeValue getInstance(Node root) {
		// TODO Auto-generated method stub
		return new BoxAttribute((Element) root);
	}
}
